package com.sea.data.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.data.entity.Order;
import com.sea.data.entity.OrderDetail;
import com.sea.data.entity.SalesForecast;
import com.sea.data.service.OrderDetailService;
import com.sea.data.service.OrderService;
import com.sea.product.service.CategoryService;
import com.sea.product.service.ChannelService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/data/orderDetail")
public class OrderDetailController {

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 修改
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxReturn delete(Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try{
            if (null != id && orderDetailService.delete(id) > 0) {
                result.setOk(true).setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 分页条件查询
     *
     * @param orderId
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByOrderId")
    public List<OrderDetail> findByOrderId(Integer orderId) {
        return orderDetailService.findByOrderId(orderId);
    }

    /**
     * 修改
     * @param orderDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(OrderDetail orderDetail) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != orderDetail && orderDetailService.usave(orderDetail) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/export")
    public void export(HttpServletResponse response, OrderDetail orderDetail)
    {

        List<OrderDetail> list = orderDetailService.export(orderDetail);
        String[] title = {"渠道","SKU","请购数量","预计交期","采购单价","请购单号","颜色","尺码","装箱量"};
        String fileName = "请购明细表" + DateUtils.dateToString(new Date(),"yyyyMMdd") + ".xls";
        String sheetName = "请购明细表";
        String[][] content = new String[list.size()][9];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            OrderDetail obj = list.get(i);
            content[i][0] = obj.getOrderChannelName();
            content[i][1] = obj.getOrderSku();
            content[i][2] = obj.getOrderNumber()+"";
            content[i][3] = obj.getOrderDate();
            content[i][4] = obj.getOrderProductPrice()+"";
            content[i][5] = obj.getOrderNo()+"";
            content[i][6] = obj.getProductColorName()+"";
            content[i][7] = obj.getProductSizeName()+"";
            content[i][8] = obj.getProductPackageNumber()+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
